package com.octopus.yangj.customized.rule;

import com.octopus.kernal.controller.SearchOperator;
import com.octopus.kernal.model.BaseStruct;
import com.octopus.kernal.service.BusinessRuleInterface;
import com.octopus.yangj.generated.controller.SearchEventExecuteCommand;
import com.octopus.yangj.generated.model.EventExecuteStruct;
import com.octopus.yangj.generated.model.EventStruct;
import com.octopus.yangj.generated.service.EventExecuteService;
import com.octopus.yangj.util.EventStatus;
import com.octopus.yangj.util.ServiceFactory;

public class EventDeleteRule implements BusinessRuleInterface{

	@Override
	public boolean isAllowed(BaseStruct struct) {
		boolean result = true;
		EventStruct event = (EventStruct)struct;
		if(EventStatus.CLOSED.getStatus().equals(event.getStatus())){
			result =  false;
		}else{
			result =  true;
		}
		SearchEventExecuteCommand searchCommand = new SearchEventExecuteCommand();
		searchCommand.setEntity(new EventExecuteStruct());
		searchCommand.setOperator(new EventExecuteStruct());
		searchCommand.getEntity().setEventId(event.getId());
		searchCommand.getOperator().setEventId(Long.valueOf(SearchOperator.EQUAL.ordinal()));
		EventExecuteService eventExecuteService = ServiceFactory.getEventExecuteService();
		int count = eventExecuteService.searchCount(searchCommand);
		if(count > 0){
			result =  false;
		}else{
			result =  result && true;
		}
		
		return result;
	}

}
